package com.leyou.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.entity.ClientInfo;
import com.leyou.auth.mapper.ClientInfoMapper;
import com.leyou.auth.service.ClientInfoService;
import com.leyou.common.exception.LyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author mySun
 * @create 2021-07-28 17:54
 */
@Service
public class ClientInfoServiceImpl extends ServiceImpl<ClientInfoMapper,ClientInfo> implements ClientInfoService {
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Value("${ly.jwt.key}")
    private String key;

    @Override
    public String getKey(String clientId, String secret) {
        ClientInfo clientInfo = this.query().eq("client_id",clientId).one();
        if(clientInfo == null || !passwordEncoder.matches(secret,clientInfo.getSecret())){
            throw new LyException(400,"服务密码错误，已报警");
        }


        return key;
    }
}
